<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="Spu名称"> {{spu.spuName}} </el-form-item>
      <el-form-item label="Sku名称">
        <el-input placeholder="Sku名称" v-model.trim="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" type="number" v-model="skuInfo.price"></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" placeholder="规格描述" rows="4" v-model.trim="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item :label="attr.attrName" v-for="(attr,index) in attrInfoList" :key="attr.id">
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option :label="attrValue.valueName" :value="`${attr.id}+${attrValue.id}`" v-for="(attrValue,index) in attr.attrValueList" :key="attrValue.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item :label="saleAttr.saleAttrName" v-for="(saleAttr,index) in spuSaleAttrList" :key="saleAttr.id">
            <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValueId">
              <el-option :label="saleAttrValue.saleAttrValueName" :value="`${saleAttr.id}+${saleAttrValue.id}`" v-for="(saleAttrValue,index) in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table style="width: 100%" border :data="spuImageList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" label="label" width="100" ></el-table-column>
          <el-table-column  prop="prop" label="图片" width="width" >
            <template slot-scope="{row,$index}">
              <img :src="row.imgUrl" style="width:100px;height:100px">
            </template>
          </el-table-column>
          <el-table-column  prop="imgName" label="名称" width="width" ></el-table-column>
          <el-table-column  prop="prop" label="操作" width="200" align="center" >
            <template slot-scope="{row,$index}">
              <el-button type="primary" v-if="row.isDefault==0" @click="changeDefault(row)">设置默认</el-button>
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveBtn">保存</el-button>
        <el-button @click="cancle"
          >取消</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      spuImageList: [], //spu图片
      spuSaleAttrList: [], //spu销售属性
      attrInfoList: [], //商品属性
      //收集sku数据的字段
      skuInfo: {
        //父组件传递过来的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //v-model绑定
        price: 0,
        skuName: "",
        weight: "",
        skuDesc: "",
        //默认图片
        skuDefaultImg: "",
        //需要自己书写代码
        //收集图片的字段
         skuImageList: [
          {
           /*  id: 0,
            imgName: "string",
            imgUrl: "string",
            isDefault: "string",
            skuId: 0,
            spuImgId: 0, */
          },
        ],
        //平台属性
        skuAttrValueList: [
         /*  {
            attrId: 0,
            valueId: 0,
          }, */
        ],
      
       //销售属性
        skuSaleAttrValueList: [
          /* {
            id: 0,
            saleAttrId: 0,
            saleAttrName: "string",
            saleAttrValueId: 0,
            saleAttrValueName: "string",
            skuId: 0,
            spuId: 0,
          }, */
        ],
       
      },
      spu:{},
      //收集图片的数据字段
      imageList:[]
    };
  },
  methods: {
    //父组件点击添加Sku的回调 发请求  （三个）
    async getData(row, cForm) {
      const spuId = row.id;
      const { category1Id, category2Id, category3Id } = cForm;
      //收集父组件给予的数据
      this.skuInfo.category3Id=category3Id
      this.skuInfo.spuId=spuId
      this.skuInfo.tmId=row.tmId
      this.spu=row

      //获取Spu图片
      let spuImageListResult = await this.$API.spu.reqSpuImageList(spuId);
      if (spuImageListResult.code == 200) {
        let list  = spuImageListResult.data;
        list.forEach(item => {
          //给每一个对象都添加一个isDefault字段
          item.isDefault=0;
        });
        this.spuImageList = list
      }
      //获取销售属性
      let spuSaleAttrListResult = await this.$API.spu.reqSpuSaleAttrList(spuId);
      if (spuSaleAttrListResult.code == 200) {
        this.spuSaleAttrList = spuSaleAttrListResult.data;
      }
      //获取商品属性
      let attrListResult = await this.$API.spu.reqAttrList(
        category1Id,
        category2Id,
        category3Id
      );
      if (attrListResult.code == 200) {
        this.attrInfoList = attrListResult.data;
      }
    },
    //table表格复选框按钮的事件
    handleSelectionChange(selection){
      //可以获取到用户选中的图片信息数组，但是缺少 isDefault字段
      this.imageList=selection
    },
    //设置默认图片按钮点击的回调，排他
    changeDefault(row){
      //把所有项目的isDefault字段设成0  自己设成1
      this.spuImageList.forEach(item=>{
        item.isDefault=0
      })
      row.isDefault=1;
      //收集默认图片的地址
      this.skuInfo.skuDefaultImg=row.imgUrl
    },
    //取消按钮
    cancle(){
      //自定义事件,通知父组件切换场景
      this.$emit('changeScenes',0)
      //清除数据
      Object.assign(this._data,this.$options.data());
    },
    //保存按钮的回调
    async saveBtn(){
      //整理参数
      //整理平台属性
      const {attrInfoList,skuInfo,spuSaleAttrList,imageList} = this;
      //整理平台属性的数据
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev,item)=>{
        if(item.attrIdAndValueId){
          const[attrId,valueId] = item.attrIdAndValueId.split('+')
          prev.push({attrId,valueId})
        }
        return prev
      },[])
      //整理销售属性的数据
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev,item)=>{
        if(item.attrIdAndValueId){
          const[saleAttrId,saleAttrValueId]=item.attrIdAndValueId.split('+')
          prev.push({saleAttrId,saleAttrValueId})
        }
        return prev
       },[])
      //整理图片数据
      skuInfo.skuImageList = imageList.map(item=>{
        return {
          imgName:item.imgName,
          imgUrl:item.imgUrl,
          isDefault:item.isDefault,
          spuImgId:item.id
        }
      })
      //发请求
      let result =  await this.$API.spu.reqAddSku(skuInfo)  
      if(result.code==200){
        this.$message({type:'success',message:'成功添加Sku'})
        this.$emit('changeScenes',0);
        //清除数据
        Object.assign(this._data,this.$options.data());
      }
      }
  },
};
</script>

<style>
</style>